Import('env', 'x86', 'arm64', 'crc32c_no_hardware', 'crc32c_cppflags', 'crc32c_cflags')

libgalerautils_env = env.Clone()

# Include paths
libgalerautils_env.Append(CPPPATH = Split('''
                                             #
                                          '''))

# C part
libgalerautils_sources = [
    'gu_abort.c',
    'gu_dbug.c',
    'gu_fifo.c',
    'gu_lock_step.c',
    'gu_log.c',
    'gu_mem.c',
    'gu_mmh3.c',
    'gu_spooky.c',
    'gu_rand.c',
    'gu_threads.c',
    'gu_hexdump.c',
    'gu_to.c',
    'gu_utils.c',
    'gu_uuid.c',
    'gu_backtrace.c',
    'gu_limits.c',
    'gu_time.c',
    'gu_init.c'
]

libgalerautils_objs = libgalerautils_env.SharedObject(libgalerautils_sources)

crc32c_env = libgalerautils_env.Clone()
crc32c_env.Append(CPPFLAGS = crc32c_cppflags)
crc32c_sources = [ 'gu_crc32c.c' ]

if not crc32c_no_hardware:
    # environment with hardware-specific flags
    crc32c_hw_env = crc32c_env.Clone()
    crc32c_hw_env.Append(CFLAGS = crc32c_cflags)
    if x86:
        libgalerautils_objs += crc32c_hw_env.SharedObject([ 'gu_crc32c_x86.c' ])
    elif arm64:
        libgalerautils_objs += crc32c_hw_env.SharedObject([ 'gu_crc32c_arm64.c' ])

libgalerautils_objs += crc32c_env.SharedObject(crc32c_sources)

libgalerautils_env.StaticLibrary('galerautils',
                                 libgalerautils_objs)

env.Append(LIBGALERA_OBJS = libgalerautils_objs)

libgalerautilsxx_env = env.Clone()

# Include paths
libgalerautilsxx_env.Append(CPPPATH = Split('''
                                               #
                                               #/common
                                            '''))

# disable old style cast warnings
libgalerautilsxx_env.Append(CXXFLAGS = ' -Wno-old-style-cast')

# C++ part
libgalerautilsxx_sources = [
    'gu_vlq.cpp',
    'gu_datetime.cpp',
    'gu_exception.cpp',
    'gu_serialize.cpp',
    'gu_logger.cpp',
    'gu_regex.cpp',
    'gu_string_utils.cpp',
    'gu_uri.cpp',
    'gu_buffer.cpp',
    'gu_utils++.cpp',
    'gu_config.cpp',
    'gu_fdesc.cpp',
    'gu_mmap.cpp',
    'gu_alloc.cpp',
    'gu_rset.cpp',
    'gu_resolver.cpp',
    'gu_histogram.cpp',
    'gu_stats.cpp',
    'gu_asio.cpp',
    'gu_debug_sync.cpp',
    'gu_thread.cpp'
]

#libgalerautilsxx_objs  = libgalerautilsxx_env.Object(
#                         libgalerautilsxx_sources)

libgalerautilsxx_sobjs = libgalerautilsxx_env.SharedObject(
                         libgalerautilsxx_sources)

#hexdump_obj  = libgalerautilsxx_env.Object('gu_hexdump++','gu_hexdump.cpp')
hexdump_sobj = libgalerautilsxx_env.SharedObject('gu_hexdump++','gu_hexdump.cpp')
uuid_sobj = libgalerautilsxx_env.SharedObject('gu_uuid++','gu_uuid.cpp')

#libgalerautilsxx_objs  += hexdump_obj
libgalerautilsxx_sobjs += hexdump_sobj
libgalerautilsxx_sobjs += uuid_sobj

#if '-DGALERA_USE_GU_NETWORK' in libgalerautils_env['CPPFLAGS']:
#    libgalerautilsxx_sources = [libgalerautilsxx_sources,
#                                'gu_resolver.cpp']

libgalerautilsxx_env.StaticLibrary('galerautils++',
                                   libgalerautilsxx_sobjs)

env.Append(LIBGALERA_OBJS = libgalerautilsxx_sobjs)
